/**
 * 语音功能兼容性检查工具
 * 用于检测浏览器对TTS和录音功能的支持情况
 */

// 浏览器兼容性检查结果接口
export interface BrowserCompatibility {
  tts: boolean;
  recording: boolean;
  mimeType: string | null;
  os: 'ios' | 'android' | 'harmony' | 'unknown';
  browser: string;
}

// iOS Safari的特殊处理
export const isIOS = (): boolean => {
  return /iPad|iPhone|iPod/.test(navigator.userAgent) && !(window as any).MSStream;
};

// 鸿蒙系统检测
export const isHarmonyOS = (): boolean => {
  return navigator.userAgent.includes('HarmonyOS') || 
         navigator.userAgent.includes('Harmony');
};

// Android检测
export const isAndroid = (): boolean => {
  return /Android/.test(navigator.userAgent);
};

/**
 * 浏览器兼容性检查工具
 */
export const checkBrowserCompatibility = (): BrowserCompatibility => {
  const tts = 'speechSynthesis' in window;
  const recording = !!(navigator.mediaDevices && window.MediaRecorder);
  const mimeType = getCompatibleMimeType();
  
  // 检测操作系统
  let os: BrowserCompatibility['os'] = 'unknown';
  if (isIOS()) os = 'ios';
  else if (isHarmonyOS()) os = 'harmony';
  else if (isAndroid()) os = 'android';
  
  // 检测浏览器
  let browser = 'unknown';
  const ua = navigator.userAgent;
  if (ua.includes('Chrome')) browser = 'chrome';
  else if (ua.includes('Safari')) browser = 'safari';
  else if (ua.includes('Firefox')) browser = 'firefox';
  else if (ua.includes('Edge')) browser = 'edge';
  
  return {
    tts,
    recording,
    mimeType,
    os,
    browser
  };
};

/**
 * 获取兼容的音频格式
 */
export const getCompatibleMimeType = (): string | null => {
  if (typeof MediaRecorder === 'undefined') {
    return null;
  }
  
  const mimeTypes = [
    'audio/webm;codecs=opus',
    'audio/mp4;codecs=mp4a',
    'audio/ogg;codecs=opus',
    'audio/wav'
  ];
  
  for (const mimeType of mimeTypes) {
    if (MediaRecorder.isTypeSupported(mimeType)) {
      return mimeType;
    }
  }
  
  return null;
};

/**
 * iOS语音合成特殊处理
 * iOS需要用户交互才能触发语音合成
 */
export const setupIOSSpeechSynthesis = (): (() => void) => {
  if (!isIOS()) {
    return () => {}; // 非iOS不需要处理
  }
  
  const handleFirstInteraction = () => {
    // 空语句触发语音合成权限
    const utterance = new SpeechSynthesisUtterance('');
    window.speechSynthesis.speak(utterance);
    window.speechSynthesis.cancel();
    
    // 移除事件监听器
    document.removeEventListener('click', handleFirstInteraction);
    document.removeEventListener('touchstart', handleFirstInteraction);
  };
  
  // 添加事件监听器
  document.addEventListener('click', handleFirstInteraction, { once: true });
  document.addEventListener('touchstart', handleFirstInteraction, { once: true });
  
  // 返回清理函数
  return () => {
    document.removeEventListener('click', handleFirstInteraction);
    document.removeEventListener('touchstart', handleFirstInteraction);
  };
};

/**
 * 获取兼容性建议信息
 */
export const getCompatibilitySuggestions = (compatibility: BrowserCompatibility): string[] => {
  const suggestions: string[] = [];
  
  if (!compatibility.tts) {
    suggestions.push('您的浏览器不支持文本朗读功能，建议使用Chrome或Safari');
  }
  
  if (!compatibility.recording) {
    suggestions.push('您的浏览器不支持录音功能，建议使用Chrome或Safari');
  }
  
  if (!compatibility.mimeType) {
    suggestions.push('无法找到合适的音频格式支持，录音功能可能受限');
  }
  
  if (compatibility.os === 'ios') {
    suggestions.push('iOS系统需要用户交互才能启动语音功能，请点击页面任意位置激活');
  }
  
  return suggestions;
};